/**
 * 		action.js
 * 			
 * This file is used for functions such as: select feature 
 * 
 *	History: 	
 *   DATE 					AUTHOR 					DESCRIPTION
 * ----------------------------------------------------------------------------------------------------------
 * 23 - 05 - 2013			NguyenNV					Update search and draw polygon
 */
/**
 * 	 Open attribute
 *	 @param layer layer that is opened attributribute
 */
function openattribute(layer){
	$('#div_open_attribute').fadeIn(300);	
	CurrentLayer.create();
	CurrentLayer.layerId = layer;
	COMBO_ID_LAYER = CurrentLayer.layerId;
	//CurrentLayer.arrAlias = new Array();
	var arrayAllFid = new Array();
	CurrentLayer.arrAlias = CommonGeo.setCurrentAlias(COMBO_ID_LAYER);
	//CurrentLayer.arrAlias = CurrentLayer.arrAlias;
	ARR_LAYER_PRIMARY = new Array();
	ARR_LAYER_PRIMARY = CommonGeo.getAttributeOfLayer(COMBO_ID_LAYER);
	for(var j=0; j<ARR_LAYER_PRIMARY.length; j++)
	{
		arrayAllFid[arrayAllFid.length] = ARR_LAYER_PRIMARY[j].attribute["fid"];
	}
	Graphic.init();
	Graphic.isShowView = false;
	Graphic.isShowAddInfo = false;
	Graphic.recordPerPage = 10;
	Graphic.divPaging = base.getElementById('footer-page1');
	Graphic.tableContent = "open_attribute";
	Graphic.displayList(arrayAllFid);
	Graphic.waitingData();
	Graphic.drawFooter();
}
/**
 * 	 Select option
 *	 @param select box id
 */
function comboSelectLayer(str){	
	CommonFuns.removeOptionCombo(str);
	var selectToolCboLayers1 = base.getElementById(str);
	if(selectToolCboLayers1!=null){
		var i = 0;
		// WFS
		var objMap = null;
		var slectedOption = document.createElement('option');
			slectedOption.text = LANGUAGE["option_Tool_select_0"];
			slectedOption.value = 0;
			slectedOption.selected = "selected";
			selectToolCboLayers1.add(slectedOption);
		for (i = 0; i < mapWFS.length; i++) {
			objMap = mapWFS[i];
			if(!objMap.Display) continue;
			var elOptNew = document.createElement('option');
			elOptNew.text = objMap.Layer_Name;
			elOptNew.value = objMap.IndexLayer;
			try {
				// not IE
				selectToolCboLayers1.add(elOptNew, null);
				
			} catch (ex) {
				// IE only
				selectToolCboLayers1.add(elOptNew);
				}
			}	
	}
	$("#fieldset_map_open_attribute select").uniform();
}
function comboSelectLayerToImport(str){	
	
	CommonFuns.removeOptionCombo(str);
	var selectToolCboLayers1 = base.getElementById(str);
	if(selectToolCboLayers1!=null){
		
		var i = 0;
		// WFS
		var objMap = null;
		var slectedOption = document.createElement('option');
			slectedOption.text = LANGUAGE["option_Tool_select_0"];
			slectedOption.value = 0;
			slectedOption.selected = "selected";
			selectToolCboLayers1.add(slectedOption);
		for (i = 0; i < mapWFS.length; i++) {
			objMap = mapWFS[i];
			if(!objMap.Display) continue;
			else
			{
				if(objMap.LayerId == LayerAssetName.hoga||objMap.LayerId == LayerAssetName.cong){
					var elOptNew = document.createElement('option');
					elOptNew.text = objMap.Layer_Name;
					elOptNew.value = objMap.IndexLayer;
					try {
						selectToolCboLayers1.add(elOptNew, null);
					} catch (ex) {
						selectToolCboLayers1.add(elOptNew);
					}
				}
			}
		}	
	}
	$("#fieldset_Map_ImportExcel select").uniform();
}

function initComboxExportShapefile(){
	var i = 0;
	// WFS
	var objMap = null;
	var select = document.getElementById("select_layer_Export_shapefile");
	CommonFuns.removeOptionCombo("select_layer_Export_shapefile");
	var slectedOption = document.createElement('option');
	slectedOption.text = LANGUAGE["option_Tool_select_0"];
	slectedOption.value = 0;
	slectedOption.selected = "selected";
	select.add(slectedOption);
	for (i = 0; i < mapWFS.length; i++) {
		objMap = mapWFS[i];
		if(!objMap.Display) continue;
		else
		{
			if(objMap.LayerId != LayerAssetName.diemKhachHang){
				var elOptNew = document.createElement('option');
				elOptNew.text = objMap.Layer_Name;
				elOptNew.value = objMap.IndexLayer;
				try {
					select.add(elOptNew, null);
				} catch (ex) {
					select.add(elOptNew);
				}
			}
		}
	}
	$(select).uniform();
}

/**
 * 	 Get value of select box
 *	 @param element object
 */
function getComboSelectLayer(element){
	
	var selectToolCboLayers1 = $(element);
	var index = selectToolCboLayers1.val();
	switch(CurrentFunction.name)
	{
		case Fuctions.openAttributes:
			if( index <= 0 )
			{
				
			}
			else
			{
				var layer =  vectorArray[index-numBaseLayer];
				CurrentLayer.create();
				CurrentLayer.layerId = layer.LayerId;
				openattribute(CurrentLayer.layerId);
				$("#div_dialog_OpenAttribute").dialog('open');
			}
			break;
		case Fuctions.importExcel:
			var layer =  vectorArray[index-numBaseLayer];
			var indexLayer = 0;
			if(index==0){
				$("#divUploadFiles").hide();
			}else
			{
				for(var i=0;i<vectorArray.length;i++){
					if(vectorArray[i].LayerId==layer.LayerId){
						if(vectorArray[i].Editor){
							indexLayer = vectorArray[i].IndexLayer;
						}
					}
				}
				map.layers[indexLayer].setVisibility(true);
				$("#divUploadFiles").show();
				switch (layer.LayerId) {
					case LayerAssetName.hoga:
						HoGaImport.layerId = layer.LayerId;
						HoGaImport.initImport(layer.LayerId);
						HoGaImport.layer = map.layers[index];
						HoGaImport.active(true);
						break;
					case LayerAssetName.cong:
						TuyenCongImport.layerId = layer.LayerId;
						TuyenCongImport.initImport(layer.LayerId);
						TuyenCongImport.layer = map.layers[index];
						TuyenCongImport.indexLayer = index;
						TuyenCongImport.active(true);
						break;	
					default:
						ImportExcel.layerId = layer.LayerId;
						ImportExcel.innit(layer.LayerId);
						ImportExcel.layer = map.layers[index];
						break;
				}
			}
			break;
		case Fuctions.exportShapeFile:
			var layer =  vectorArray[index-numBaseLayer];
			ExportShapeFile.index = index;
			ExportShapeFile.layerId = layer.LayerId;
			ExportShapeFile.layer = map.layers[index];
			ExportShapeFile.activeSelect();
			break;
		case Fuctions.importShapeFile:
			if(index<=0){
				$("#divUploadFiles_shapefile").hide();
			}else{
				var layer =  vectorArray[index-numBaseLayer];
				ImportShapeFile.drawFormUpload(layer.LayerId);
				$("#divUploadFiles_shapefile").show();
			}
			break;
		default:
			break;
	}
}
function getComboSearchGeom(element){
	var functionId = 0;
	ScanFeature.close();
	ScanFeature.closeDrawPolygone();
	MapAction.HideWFSAll();
	if(element.value == 0)
	{
		functionId = Fuctions.none;
	}
	else 
	{	
		if(element.value == "1")
			functionId = Fuctions.selectFeature;
		else if(element.value == "2")
			functionId = Fuctions.searchInsidePolygon;
		else if(element.value == "3")
			functionId = Fuctions.searchByDrawPolygone;		
	}
	switch(functionId)
	{
		case Fuctions.none:
			break;
		case Fuctions.selectFeature:
			MapAction.ShowWFSAll();
			var div3 = base.getElementById(DivObject.DIV_SEARCH_BY_SELECTION);
			div3.style.display = "block";
			comboSelectLayer('select_Tool_cboLayers_geom');
			break;
		case Fuctions.searchInsidePolygon:
			MapAction.ShowWFSAll();
			var div3 = base.getElementById(DivObject.DIV_SEARCH_INSIDE_DISTRICT);
			div3.style.display = "block";
			var combo = base.getElementById(DivObject.SELECT_TOOL_COMBO_DISTRICT);
			CommonFuns.removeOptionCombo(DivObject.SELECT_TOOL_COMBO_DISTRICT);
			Graphic.comboPhuongXa(combo,"540");
			SearchingAction.drawListOfAsset(base.getElementById(DivObject.DIV_LIST_ASSET_INSIDE_DISTRICT));
			break;
		case Fuctions.searchByDrawPolygone:
			MapAction.ShowWFSAll();
			var div3 = base.getElementById(DivObject.DIV_SEARCH_BY_DRAW_POLYGONE);
			div3.style.display = "block";
			ScanFeature.create();
			ScanFeature.drawPolygon(true);
			break;
		default:
			break;
	}
	$(".form-mapleft-menu select").uniform();
} 